﻿using System.Configuration;
using System.IO;
using System.Linq;
using System.Web.Mvc;
using Epam.PhotoGallery.Bussines.Configuration;
using Epam.PhotoGallery.Interfaces.DataTransferObjects;
using Epam.PhotoGallery.Site.Models;

namespace Epam.PhotoGallery.Site.Controllers
{
	public class MainController : Controller
	{


		public ActionResult Index()
		{
			GalleryControlBlockSizeSection GalleryBlok = (GalleryControlBlockSizeSection)ConfigurationManager.GetSection(GalleryControlBlockSizeSection.GalleryControlBlockSizeSectionName);
		

			GalleryControlModel GCModel = new GalleryControlModel();
			
			RepositorySection repository = (RepositorySection)ConfigurationManager.GetSection(RepositorySection.RepositorySectionName);

			var path = repository.IsRelative ? Path.Combine(Server.MapPath("~"), repository.Path) : repository.Path;
			TimeLine timeLine = GCModel.GetData(path);
			TimeItem[] elements = timeLine.Events.ToArray<TimeItem>();
			
			int lastElements = elements.Count();
			
			for (int i = GalleryBlok.Count; i > 0 ; i--)
			{
				var item = elements[lastElements - i];

				GCModel.Controls.Add(item);
			}

			return View(GCModel);
		}

	}
}
